<%--
  Created by IntelliJ IDEA.
  User: admin
  Date: 2016/3/24
  Time: 15:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" import="com.mos.support.enumeration.AccessoryType" %>
<%@include file="/WEB-INF/view/common/jsp_top.jsp" %>
<form id="menu_add_fm" method="post">
    <table  width="100%" border="0" class="t4">
        <tr>
            <td class="text">
                菜单名称：
            </td>
            <td class="edit">
                <input type="text" name="name"   class="easyui-validatebox "  data-options="required:true" />
            </td>
            <td class="text">
                菜单地址：
            </td>
            <td class="edit">
                <input type="text" name="path" class="easyui-validatebox"  data-options="required:true"/>
            </td>
        </tr>
        <tr>
            <td class="text">
                上级菜单：
            </td>
            <td class="edit">
                <input type="text" name="pid" class="easyui-validatebox" data-options="required:true" />
            </td>
            <td class="text">
                权限标识：
            </td>
            <td class="edit">
                <input type="text" name="permission" class="easyui-validatebox "  data-options=""/>
            </td>
        </tr>
        <tr>
            <td class="text">
                排序号：
            </td>
            <td class="edit">
                <input type="text" name="sortNo"  class="easyui-numberbox"  data-options="value:0,min:0,max:99999,precision:0"  />
            </td>
            <td class="text">
                是否显示：
            </td>
            <td class="edit" align="center">
                <input type="radio" name="isShow" value="1" /><label>是</label>
                <input type="radio" name="isShow" value="0" checked="checked"/><label>否</label>
            </td>
        </tr>
        <tr>
            <td class="text">
                图标来源
            </td>
            <td class="edit">
                <input type="radio" name="icon_source" id="system_define" value="<%=AccessoryType.SYSTEM.getCode() %>">系统自带
                <input type="radio" name="icon_source" id="custom_define" value="<%=AccessoryType.CUSTOM_DEFINE.getCode() %>" checked="checked" >自定义
            </td>
            <td class="text">
            </td>
            <td class="edit">
            </td>
        </tr>
        <tr id="icon" style="display: none">
            <td class="text">
                图标文件
            </td>
            <td class="edit" colspan="3">
                <div id="icon_custom_define" style="display: none">
                    <sys:upload id="icon" fieldId="" fieldName="icon" fileTypeDesc="*.jpg;*.png" fileTypeExts="*.jpg;*.png" readonly="readonly" required="true" multi="true"></sys:upload>
                </div>
                <div id="icon_system_define" style="display: none;">
                    <div style="margin-top: 2px;margin-bottom: 2px;">
                       已选文件： <input type="text" id="systemIconName" name="systemIconName" value="">
                        <input type="hidden" id="systemIconPath" name="systemIconPath" value="">
                    </div>
                    <div id="icons" style="max-height:200px;width: 500px;overflow: auto">
                    </div>
                </div>
            </td>
        </tr>

        <tr>
            <td class="btn" style="height: 40px;" colspan="4" align="center">
                <a href="javascript:void(0)" class="easyui-linkbutton" id="menu_add_btn" >保存</a>
            </td>
        </tr>
    </table>
</form>
<script type="text/javascript">
    j(document).ready(function(){
        _chooseIcon();

        //保存
        j("#menu_add_btn").click(function() {
            j('#menu_add_fm').form('submit',{
                url:'menu/save',
                ajax:true,//默认true
                onSubmit:function(){
                   /* var isvalid=j(this).form('validate');
                    if(isvalid) {
                        j.messager.progress({
                            title:'温馨提示',
                            msg:'正在处理中...'
                        });
                    }
                    return isvalid;*/
                },
                success:function (data){
                    var data = eval('('+data+')');
                    j.messager.progress('close');
                    j.messager.show({
                        title: '温馨提示',
                        msg: data.message,
                        showType: 'show'
                    });
                    parent.j('#addPage').dialog('close',false);

                },
                onLoadError: function () {
                    j.messager.progress('close');
                    j.messager.show({
                        title: '温馨提示',
                        msg: '保存失败，请重试！',
                        showType: 'show'
                    });
                }
            });
        });

    });

    function _chooseIcon(){
        j("input[type='radio'][name='icon_source']").each(function(){
            var old = null;
            var id= j(this).attr("id");

            if(this.checked){
                old = this; //如果当前对象选中，保存该对象
                _changeIconSourceDiv(id);
            }
            this.onclick = function(){
                if(this == old){
                    this.checked = false;
                    old = null;
                }else {
                    old = this;
                }
                _chooseIcon();
            }
        });
    }

    function _changeIconSourceDiv(id) {
        if(("icon_" + id) == "icon_system_define"){
            _loadSystemIcon();
        }
        j("#icon").show();
        j("div[id^='icon_']").hide();
        j("#icon_" + id).show();
    }

    function _uploadSystemIcon(imgObj){
        var _name = j(imgObj).attr("name");
        var _path = j(imgObj).attr("path");
        j.ajax({
            url : 'accessory/upload',
            type : 'post',
            async : false,//同步
            data:{"name":_name,"filePath":_path},
            success : function(data) {
                j("#systemIconName").attr("value",data.originalName);
                j("#systemIconPath").attr("value",data.tmpPath);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                console.log(textStatus);
            }
        });
    }

    function _loadSystemIcon(){
        j.ajax({
            url : 'icon/load',
            type : 'GET',
            async : false,//同步
            success : function(data) {
                var _imgHtml = "";
                for(var i=0;i<data.length;i++){
                    _imgHtml += "<img src='"+data[i].showPath+"' style='float: left' name='"+data[i].name+"' path='"+data[i].path+"' onclick='_uploadSystemIcon(this)'/>"
                }
                j("#icons").html(_imgHtml);
            },
            error : function(XMLHttpRequest, textStatus, errorThrown) {
                console.log(textStatus);
            }
        });
    }


</script>
<%@include file="/WEB-INF/view/common/jsp_bottom.jsp" %>
